Thread: Difficulty with text file reader

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    11

    Difficulty with text file reader

    I wrote a program to sort through 300,000 lines of formatted data in a text file to try to save some time. I am having a few problems.
    1. When compiling i get a warning I have never seen before.

    warning: unknown escape sequence: '\040'

    2. It's either locking up or getting into an infinite loop I cant figure out. When I put it through GDB and did backtrace it showed this

    0 ... in __kernel_vsyscall ()
    1 ... in read () from /lib/libc.do.6
    2 ... in _IO_file_underflow () from /lib/libc.so.6
    3 ... in _IO_default_uflow () from /lib/libc.so.6
    4 ... in __uflow () from /lib/libc.so.6
    5 ... in _IO_getline_info () from /lib/libc.so.6
    6 ... in _IO_getline () from /lib/libc.so.6
    7 ... in fgets () from /lib/libc.so.6
    8 ... in find_subrec

    Any help would be greatly appreciated. I'll post some of the code if that would help but it is almost 400 lines. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, code would help immensely, as would providing a line number for the warning and highlighting said line in the code. You only need to post the relevant section/function. It will probably help to read up on escape sequences in C, particularly the parts about escaping a backslash and octal escape sequences.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That escape sequence is octal for ASCII value of the space character. It means you probably did something like this in your source code:
    Code:
    file = fopen("<path>\ filename") <----notice the space
    vice
    file=fopen("<path>\\filename") <-------no space
    This could be anywhere you used a string literal with a backslash followed by a space vice a character.

    EDIT: If you are going to post code just post the relevant line/section pointed to by your compiler, not all 400 lines please.
    Last edited by AndrewHunter; 08-14-2011 at 08:12 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Most compilers give you a line number as part of the error message... it's probably on that line.

    I'm a little curious though... if you wrote the code, how does an unknown escape sequence get into it? Unless you meant /040 ... as in division.

    Post the code near the problem line (a complete function if possible) lets have a look....

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    PING! another triple header!

    We should start a baseball team... we'd be unbeatable!
    Last edited by CommonTater; 08-14-2011 at 08:11 PM. Reason: ... and, I really do need to learn how to spell!

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    I'm a little curious though... if you wrote the code, how does an unknown escape sequence get into it? Unless you meant /040 ... as in division.
    Because he accidentally put it in there. That is the problem with escape sequences. They are designated by a \ which is also used for path names if used incorrectly.

    EDIT: No kidding. We would kick ass.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    That escape sequence is octal for ASCII value of the space character. It means you probably did something like this in your source code:
    Code:
    file = fopen("<path>\ filename") <----notice the space
    vice
    file=fopen("<path>\filename") <-------no space
    This could be anywhere you used a string literal with a backslash followed by a space vice a character.

    EDIT: If you are going to post code just post the relevant line/section pointed to by your compiler, not all 400 lines please.
    That path would require double slashing (\\) so yes that's a posibility.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Because he accidentally put it in there. That is the problem with escape sequences. They are designated by a \ which is also used for path names.
    Answered before you asked!

    Dang, my crystal ball is in good form tonight...

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Answered before you asked!

    Dang, my crystal ball is in good form tonight...

    LOL...no kidding.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    LOL...no kidding.
    I just KNEW you were going to say that...

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    I just googled the escape sequence and fixed that. Here is the function that appears to be causing the problem

    Code:
    void find_subrec(FILE* ifp, FILE* ofp, char* line)
    {
      char* subrec = "STARTSUBRECORD: IVSUBREC";
      char* partnum = "PARTNUMB:";
      char* temp = malloc_array(STR);
    
      fgets( temp, 50, ifp);
      
      while( (strncmp( temp, subrec, 24) != 0) && (strncmp( temp, partnum, 9) != 0 ) )
        fgets( temp, 50, ifp);
    
      while( (strncmp( temp, subrec, 24) != 0) && (strncmp( temp, partnum, 9) == 0 ) )
      {
        fprintf( ofp, "%s\n", line);
        printf("line to file\n");
    
        free(temp);
    
        line = clear_array(line, LINE);
       
        return;
      }
      free(temp);
      
      sort_vendor(ifp, ofp, line);
      
      return;
    }
    What I am trying to do is read through the file for lines marked like VENDOR: and PARTNUMB: then copy those lines to a string with no newline characters so when it gets printed in the new file all the information is in neat columns that I can open in excel. The system the information is coming out of wont export to spreadsheet, only to text files.

  12. #12
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    Wow you guys are fast and good, the escape sequence was a \t\ when i meant \t in a string, I should have googled that before I posted that issue sorry for that one.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so what line number is the compiler complaining about?

    EDIT: Ok good.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    The compiler isn't complaining at all about that function. I put in the printf ("line to file"); to see how far it was getting. It prints line to file 5 times before it just stops.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So long as ifp and ofp are properly opened, I don't see anything here that should be a problem...

    The only thing that might be messing you up is that you are using fgets() in a function that is handing in a line to be worked on and this might displace your file pointers causing you to miss some file data... You need to create a sub-set file to test that.

    On a point of form... not really an error, your function seems to be doing multiple things and might be better broken down into smaller functions... eg. 1 to locate and extract the text, 1 to handle buffer clears etc. What you have isn't wrong... but generally the best functions do one thing and do it well.
    Last edited by CommonTater; 08-14-2011 at 08:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. File I/O difficulty
    By Madshan in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2005, 02:31 AM
  3. file reader
    By Lord CyKill in forum C Programming
    Replies: 1
    Last Post: 04-11-2003, 05:31 AM
  4. Replies: 4
    Last Post: 06-22-2002, 12:23 PM
  5. ini file reader/writer
    By cozman in forum Game Programming
    Replies: 7
    Last Post: 06-11-2002, 10:09 AM